package com.jd.impl;

import com.jd.dao.AddressDao;
import com.jd.entity.AddressEntity;
import com.jd.service.AddressService;
import com.jd.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressImpl implements AddressService {
    @Autowired
    AddressDao addressDao;

    @Override
    public List<AddressEntity> list(String userId) {
        return addressDao.list(userId);
    }

    @Override
    public void delById(String id) {
        addressDao.delById(id);
    }

    @Override
    public AddressEntity getById(String id) {
        return addressDao.getById(id);
    }

    @Override
    public void save(AddressEntity addressEntity) {
        if(StringUtils.isNotEmpty(addressEntity.getId())){
            addressEntity.setUpdateTime(new Date());
            addressDao.update(addressEntity);
        }else{
            String mid= IdUtil.getIdByUUId();
            addressEntity.setId(mid);
            addressEntity.setCreateTime(new Date());
            addressDao.insert(addressEntity);
        }
    }
}
